package com.erp.app.common.search.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import com.erp.app.common.ProjectConstants;
import com.erp.app.common.search.beans.SearchVO;
import com.erp.framework.exceptions.FindRecordException;
import com.erp.framework.util.BeanUtil;
import com.erp.framework.util.CommonUtil;
import com.erp.framework.util.DbObjectUtil;
import com.erp.framework.util.logger.Logger;

public class SearchDAO implements java.io.Serializable {

    protected Object getResultSetValues(ResultSet rs)

    throws SQLException {

        SearchVO searchVO = new SearchVO();
        ResultSetMetaData md = rs.getMetaData();
        for (int i = 1; i <= md.getColumnCount(); ++i) {
            Object value = new DbObjectUtil().getResultSetInterpreter(md.getColumnType(i)).interpret(rs, i);
            if (i <= 50) BeanUtil.setPropertyDefault(searchVO, "column" + i, CommonUtil.getString(value));
            searchVO.getColumns().put(md.getColumnName(i), value);
        }

//        searchVO.setColumn1(rs.getString(1));
//
//        searchVO.setColumn2(rs.getString(2));
//
//        searchVO.setColumn3(rs.getString(3));
//
//        searchVO.setColumn4(rs.getString(4));
//
//        searchVO.setColumn5(rs.getString(5));
//
//        searchVO.setColumn6(rs.getString(6));
//
//        searchVO.setColumn7(rs.getString(7));
//
//        searchVO.setColumn8(rs.getString(8));
//
//        searchVO.setColumn9(rs.getString(9));
//
//        searchVO.setColumn10(rs.getString(10));
//
//        searchVO.setColumn11(rs.getString(11));
//
//        searchVO.setColumn12(rs.getString(12));
//
//        searchVO.setColumn13(rs.getString(13));
//
//        searchVO.setColumn14(rs.getString(14));
//
//        searchVO.setColumn15(rs.getString(15));
//
//        searchVO.setColumn16(rs.getString(16));
//
//        searchVO.setColumn17(rs.getString(17));
//
//        searchVO.setColumn18(rs.getString(18));
//
//        searchVO.setColumn19(rs.getString(19));
//
//        searchVO.setColumn20(rs.getString(20));
//
//        searchVO.setColumn21(rs.getString(21));
//
//        searchVO.setColumn22(rs.getString(22));
//
//        searchVO.setColumn23(rs.getString(23));
//
//        searchVO.setColumn24(rs.getString(24));
//
//        searchVO.setColumn25(rs.getString(25));
//
//        searchVO.setColumn26(rs.getString(26));
//
//        searchVO.setColumn27(rs.getString(27));
//
//        searchVO.setColumn28(rs.getString(28));
//
//        searchVO.setColumn29(rs.getString(29));
//
//        searchVO.setColumn30(rs.getString(30));
//
//        searchVO.setColumn31(rs.getString(31));
//
//        searchVO.setColumn32(rs.getString(32));
//
//        searchVO.setColumn33(rs.getString(33));
//
//        searchVO.setColumn34(rs.getString(34));
//
//        searchVO.setColumn35(rs.getString(35));
//
//        searchVO.setColumn36(rs.getString(36));
//
//        searchVO.setColumn37(rs.getString(37));
//
//        searchVO.setColumn38(rs.getString(38));
//
//        searchVO.setColumn39(rs.getString(39));
//
//        searchVO.setColumn40(rs.getString(40));
//
//        searchVO.setColumn41(rs.getString(41));
//
//        searchVO.setColumn42(rs.getString(42));
//
//        searchVO.setColumn43(rs.getString(43));
//
//        searchVO.setColumn44(rs.getString(44));
//
//        searchVO.setColumn45(rs.getString(45));
//
//        searchVO.setColumn46(rs.getString(46));
//
//        searchVO.setColumn47(rs.getString(47));
//
//        searchVO.setColumn48(rs.getString(48));
//
//        searchVO.setColumn49(rs.getString(49));
//
//        searchVO.setColumn50(rs.getString(50));

        return searchVO;

    }

    public ArrayList searchSearch(String query, Connection conn) throws FindRecordException {
        return searchSearch(query, conn, false);
    }

    public ArrayList searchSearch(String query, Connection conn, boolean isSizeFilterOn)

    throws FindRecordException

    {

        ArrayList rslt = new ArrayList();

        Statement stmt = null;

        ResultSet rs = null;

        try {

            stmt = conn.createStatement();
            stmt.setMaxRows(ProjectConstants.DB_ROW_NUM);
            rs = stmt.executeQuery(query);

            if (rs != null) {
                while (rs.next()) {
                    rslt.add((SearchVO) getResultSetValues(rs));

                }

            }

        }

        catch (Exception e) {

        	Logger.logExceptions(this, e);
        }

        finally {

            try {

                if (rs != null)
                    rs.close();

                if (stmt != null)
                    stmt.close();

            }

            catch (Exception e) {
            }

        }

        return rslt;

    }

}
